import axios from 'axios'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

const service = axios.create({
    baseURL: '',
    timeout: 8000
})

service.interceptors.request.use(
    config => {
        let token = '';
        // TODO 此处从store中获取token
        if(token){
           config.headers['Authorization'] = 'Bearer '+token;
        }
        return config;
    },
    error => {
        console.log(error)
        Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        const code = response.data.code;
        if (code == '0000'){
            return response.data
        }
        else{
            return Promise.reject('error')
        }
    },
    error => {
       let code = 0;
       try{
           code = error.response.status
       }catch (e){

       }
       switch (code) {
           case 500:
               break;
           case 401:
               break;
           case 302:
               break;
           default:
               return Promise.reject(error)
               break;
       }
    }
)

export default service;